#include <WiFi.h>
#include "value.h"
 
const char *ssid = "yangzhiqin"; //你的网络名称
const char *password = "12345678"; //你的网络密码

const char *host = "18608f8u76.iok.la"; //欲访问的域名
void setup()
{
    Serial.begin(115200);
    Serial.println();

    WiFi.mode(WIFI_STA);
    WiFi.setSleep(false); //关闭STA模式下wifi休眠，提高响应速度
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println("WIFI Connected");
    Serial.print("IP Address:");
    Serial.println(WiFi.localIP());


    
    WiFiClient client; //声明一个客户端对象，用于与服务器进行连接

    Serial.println("尝试访问服务器");
    
    String data = "name=严霜&id=1120172019";//待发送的数据
    int length = data.length();//获取数据长度
    //post数据报
    String postRequest =
                         (String)("POST ") + "/"+"homework/Send"+" HTTP/1.1\r\n" +
                         "Host: " + host + "\r\n" +
                         "Connection: Keep Alive\r\n" +
                         "Content-Length: " + length + "\r\n" +
                         "Accept: */*\r\n" +
                         "Origin: http://"+host+"\r\n" +
                          "Upgrade-Insecure-Requests: 1"+"\r\n" +
                         "Content-Type: application/x-www-form-urlencoded;" + "\r\n" +
                         "User-Agent: ESP32\r\n" +
                          "Accept-Encoding: gzip, deflate"+"\r\n" +
                          "Accept-Language: zh-CN,zh;q=0.9"+"\r\n" +                    
                         "\r\n" +
                         data + "\r\n";                       
    if (client.connect(host, 80)) //这里包含了TCP握手、连接端口的代码封装
    {
      Serial.println("访问成功");
      String str = postRequest;
      Serial.println(str);
      client.print(str);
      //以下代码将收到的数据按行打印输出
      while (client.connected() || client.available()) //如果已连接或有收到的未读取的数据,则循环接收并输出
      {
          if (client.available()) //如果有数据可读取，则打印
          {
              String line = client.readStringUntil('\n'); //按行读取数据
              Serial.println(">>"+line);//串口打印数据
          }
      }
      client.stop(); //关闭当前连接
    }
    else //访问失败
    {
        Serial.println("访问失败");
        client.stop(); //关闭当前连接
    }
    delay(10000);
}

void loop()
{
}
